package cn.org.xwb.bookmall.user.controller;

import cn.org.xwb.bookmall.model.constant.AccountConst;
import cn.org.xwb.bookmall.model.mapper.LiuImgMapper;
import cn.org.xwb.bookmall.model.mapper.LiuMapper;
import cn.org.xwb.bookmall.model.po.*;
import cn.org.xwb.bookmall.model.service.impl.LiuServiceImpl;
import cn.org.xwb.bookmall.model.vo.*;
import cn.org.xwb.bookmall.user.annotation.IsLoginParam;
import cn.org.xwb.common.encrypt.annotation.des.DesEn;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDe;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDeObj;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDeParam;
import cn.org.xwb.common.encrypt.constant.EncryptConstant;
import cn.org.xwb.common.error.annotation.ServerErrorCatch;
import cn.org.xwb.common.model.vo.RespResult;
import cn.org.xwb.common.page.dto.PageResult;
import cn.org.xwb.common.valid.annotation.nu.NotNull;
import cn.org.xwb.common.valid.annotation.nu.NotNullObj;
import cn.org.xwb.common.valid.annotation.nu.NotNullParam;
import cn.org.xwb.common.valid.annotation.str.StrNotNull;
import cn.org.xwb.common.valid.annotation.str.StrNotNullObj;
import cn.org.xwb.common.valid.annotation.str.StrNotNullParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/liu")
public class UserLiuController {

    @Autowired
    private LiuMapper liuMapper;

    @Autowired
    private LiuImgMapper liuImgMapper;

    @Autowired
    private LiuServiceImpl liuService;

    @PostMapping("/add")
    @ServerErrorCatch
    @StrNotNull
    @NotNull
    @RsaDe
    @IsLoginParam
    @Transactional
    public RespResult<Object> addLiu(
            @RequestParam(AccountConst.ACCOUNT_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String account,
            @NotNullObj @StrNotNullObj @RsaDeObj
            AddLiuVo addLiuVo
    ) {
        Liu liu = new Liu();
        liu.setUserId(addLiuVo.getUserId());
        liu.setContent(addLiuVo.getContent());
        liu.setCreateTime(new Date());
        int res = liuMapper.insert(liu);
        if (res < 1) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RespResult.fail("发表留言失败");
        }
        if (Objects.nonNull(addLiuVo.getImg())) {
            for (String s : addLiuVo.getImg()) {
                LiuImg liuImg = new LiuImg();
                liuImg.setLiuId(liu.getId());
                liuImg.setImgUrl(s);
                res = liuImgMapper.insert(liuImg);
                if (res < 1) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return RespResult.fail("发表留言失败");
                }
            }
        }
        List<LiuVo> liuVos = liuService.findAndCacheAll();
        if (Objects.isNull(liuVos)) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RespResult.fail("发表留言失败");
        }
        return RespResult.success("发表留言成功");
    }

    @GetMapping("/page")
    @ServerErrorCatch
    @StrNotNull
    @NotNull
    @RsaDe
    @DesEn
    public RespResult<Object> getLiuPage(
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String desKey,
            @RequestParam("current") @NotNullParam
            Integer current,
            @RequestParam("pageSize") @NotNullParam
            Integer pageSize,
            @RequestParam(value = "userId",required = false)
            Long userId,
            @RequestParam(value = "kw", required = false)
            String kw
    ) {
        PageResult<List<LiuVo>> liuPage = liuService.getLiuPage(current, pageSize, userId, kw);
        return RespResult.success("获取留言列表成功", liuPage);
    }

    @PostMapping("/del")
    @ServerErrorCatch
    @StrNotNull
    @NotNull
    @RsaDe
    @IsLoginParam
    public RespResult<Object> delLiu(
            @RequestParam(AccountConst.ACCOUNT_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String account,
            @NotNullParam
            Long id
    ) {
        Boolean res = liuService.delLiu(id);
        if (!res) {
            return RespResult.fail("删除留言失败");
        }
        return RespResult.success("删除留言成功");
    }

}
